//
//  ZYProblemRecoverThreeVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/30.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYProblemRecoverThreeVC.h"
#import "ZYRecoverPasswordViewModel.h"
#import "ZYLoginMainVC.h"

@interface ZYProblemRecoverThreeVC ()
@property(nonatomic,strong) UITextField *passwordTextField;
//@property(nonatomic,strong) UITextField *rePasswordTextField;
@property(nonatomic,strong) UIButton *confirmBtn;
@property(nonatomic,strong) ZYRecoverPasswordViewModel *viewModel;
@end

@implementation ZYProblemRecoverThreeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    [self setupTap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PROBLEM_RECOVER_CONTROLLER_TITLE];
}

-(void)setupView{
    CGFloat left = 20;
    CGFloat height = 40;
    UITextField *passwordTextField = [[UITextField alloc] init];
    passwordTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
    [self.view addSubview:passwordTextField];
    passwordTextField.centerX = self.view.centerX;
    passwordTextField.top = self.navigationView.bottom + 20;
    [self setupTextField:passwordTextField];
    [self setLeftView:passwordTextField];
    
//    UITextField *rePasswordTextField = [[UITextField alloc] init];
//    rePasswordTextField.size = CGSizeMake(ZYSCREEN_W - left * 2, height);
//    [self.view addSubview:rePasswordTextField];
//    rePasswordTextField.centerX = self.view.centerX;
//    rePasswordTextField.top = passwordTextField.bottom + 26;
//    [self setupTextField:rePasswordTextField];
//    [self setLeftView:rePasswordTextField];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.bounds = passwordTextField.bounds;
    [self.view addSubview:confirmBtn];
    confirmBtn.centerX = self.view.centerX;
    confirmBtn.top = passwordTextField.bottom + 65;
    [ZYUtils setGradualChangingColorWithButton:confirmBtn size:confirmBtn.size];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    
    
    _confirmBtn = confirmBtn;
    _passwordTextField = passwordTextField;
//    _rePasswordTextField = rePasswordTextField;
    [self setupTitle];
}

-(void)setupTextField:(UITextField *)textFiled{
    textFiled.layer.borderWidth = 1;
    textFiled.layer.borderColor = RGBA(235, 235, 235, 1).CGColor;
    textFiled.layer.cornerRadius = 5;
    textFiled.layer.masksToBounds = YES;
    textFiled.secureTextEntry = YES;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
}

/**
 初始化标题
 */
-(void)setupTitle{;
    NSString *passwordPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_PROBLEM_RECOVERY_NEW_PASSWORD_PLACEHOLDER];
//    NSString *rePasswordPlaceholder = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_LOGIN_RE_PASSWORD_PLACEHOLDER];
    NSString *confirmBtnTitle = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE];
    [_confirmBtn setTitle:confirmBtnTitle forState:UIControlStateNormal];
    _passwordTextField.placeholder = passwordPlaceholder;
//    _rePasswordTextField.placeholder = rePasswordPlaceholder;
}

/**
 设置左视图
 @param textField texField
 */
-(void)setLeftView:(UITextField *)textField{
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 35, textField.height)];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.leftView = leftView;
}

#pragma mark -Event
//找回密码
-(void)doConfirm:(id)button{
    [self dismissKeyboard];
    self.viewModel.phone = _phone;
    self.viewModel.problemAnswer = _answer;
    self.viewModel.problemId = _problemId;
    self.viewModel.password = _passwordTextField.text;
//    self.viewModel.rePassword = _rePasswordTextField.text;
    @weakify(self);
    [[self.viewModel.problemCommand execute:nil] subscribeNext:^(id x) {
        @strongify(self);
        [[ToastHelper sharedToastHelper] toastSuccess:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_MODIFY_PASSWORD_SUCCESS]];
        ZYLoginMainVC *vc = [[ZYLoginMainVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } error:^(NSError *error) {
        [[ToastHelper sharedToastHelper] toastError:[NSErrorHelper handleErrorMessage:error]];
    }];
    //监听状态
    [[self.viewModel.problemCommand.executing skip:1] subscribeNext:^(id x) {
        BOOL executing = [x boolValue];
        ((UIButton *)button).enabled = !executing;
        [[ToastHelper sharedToastHelper] setSimleProgressVisiable:executing];
    }];
}

#pragma mark - Getter
-(ZYRecoverPasswordViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ZYRecoverPasswordViewModel alloc] init];
    }
    return _viewModel;
}
@end
